package com.zlt.appparking.service.impl;

import com.zlt.appparking.entity.Recharge;
import com.zlt.appparking.entity.UserWallet;
import com.zlt.appparking.entity.UserWalletExample;
import com.zlt.appparking.mapper.RechargeMapper;
import com.zlt.appparking.mapper.UserWalletMapper;
import com.zlt.appparking.service.BalanceService;
import com.zlt.appparking.utils.SnowFlakeUtil;
import com.zlt.appparking.vo.ActiveUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class BalanceServiceImpl implements BalanceService {

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public BigDecimal selectBalance() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后查看");
        }
        UserWalletExample userWalletExample = new UserWalletExample();
        userWalletExample.createCriteria().andAuIdEqualTo(activeUser.getAuId());
        //查询用户钱包信息
        List<UserWallet> userWallets = userWalletMapper.selectByExample(userWalletExample);
        if(userWallets != null && userWallets.size()>0){
            return userWallets.get(0).getUwMoney();//返回钱包余额
        }
        return null;//返回钱包余额

    }

    @Override
    @Transactional
    public String addCharge(BigDecimal bd) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后充值");
        }
        boolean flag = false;
        //生成订单号
        SnowFlakeUtil snowFlake = new SnowFlakeUtil(0);
        long l = snowFlake.nextId();//生成订单
        Recharge recharge = new Recharge();
        recharge.setrNo(l+"");//设置订单号
        recharge.setAuId(activeUser.getAuId());//用户id
        recharge.setAuMoney(bd);//充值金额
        recharge.setrStart(new Date());//订单生成时间
        recharge.setIsRecharge((byte) 0);//充值,status默认为未支付
        int i = rechargeMapper.insertSelective(recharge);
        //todo 调用支付宝接口，
        String form = "";

        return form;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeWallet(BigDecimal bd){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后充值");
        }
        //查询当前钱包余额
        UserWalletExample userWalletExample = new UserWalletExample();
        userWalletExample.createCriteria().andAuIdEqualTo(activeUser.getAuId());
        //查询用户钱包信息
        List<UserWallet> userWallets = userWalletMapper.selectByExample(userWalletExample);
        UserWallet userWallet = new UserWallet();
        BigDecimal b = new BigDecimal(0);
        if(userWallets != null && userWallets.size()>0){
            userWallet = userWallets.get(0);
            b = userWallet.getUwMoney();
        }
//        recharge.setrStatus((byte) 1);
//        recharge.setrPaytime(new Date());
//        rechargeMapper.updateByPrimaryKeySelective(recharge);//修改订单状态,支付时间
        userWallet.setUwMoney(b.add(bd));
        userWalletMapper.updateByPrimaryKeySelective(userWallet);//修改钱包余额
        return true;
    }

    @Override
    public String withdrawalCharge(BigDecimal bd) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后再提现");
        }
        //生成订单号
        SnowFlakeUtil snowFlake = new SnowFlakeUtil(0);
        long l = snowFlake.nextId();
        //生成订单
        Recharge recharge = new Recharge();
        recharge.setrNo(l+"");//设置订单号
        recharge.setAuId(activeUser.getAuId());//用户id
        recharge.setAuMoney(bd);//提现金额
        recharge.setrStart(new Date());//订单生成时间
        recharge.setIsRecharge((byte) 1);//提现
        recharge.setrStatus((byte) 3);//状态设置为提现中
        int i = rechargeMapper.insertSelective(recharge);
        // TODO: 2022/10/3 调用支付宝接口，进行提现操作
        String form ="";
        return form;
    }

    @Override
    public boolean defWallet(BigDecimal bd){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser == null){
            throw new RuntimeException("请登录后再提现");
        }
        //查询当前钱包余额
        UserWalletExample userWalletExample = new UserWalletExample();
        userWalletExample.createCriteria().andAuIdEqualTo(activeUser.getAuId());
        //查询用户钱包信息
        List<UserWallet> userWallets = userWalletMapper.selectByExample(userWalletExample);
        UserWallet userWallet = userWallets.get(0);
        BigDecimal b = userWallet.getUwMoney();//用户当前余额
        //判断提现金额是否超出余额
        if (!(b.compareTo(bd) > -1)){
            throw new RuntimeException("余额不足");
        }
//        recharge.setrStatus((byte) 4);
//        recharge.setrPaytime(new Date());//订单完成时间
//        rechargeMapper.updateByPrimaryKeySelective(recharge);//修改订单状态,支付时间
        userWallet.setUwMoney(b.subtract(bd));//减去提现金额
        userWalletMapper.updateByPrimaryKeySelective(userWallet);//修改钱包余额
        return true;
    }
}
